using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cysharp.Threading.Tasks;

public partial class MenuPanel : UIPanel
{
    private const string path = "Assets/Prefab/MenuPanel.prefab";

    public MenuPanel() : base(new UIType(path))
    {
        m_IsChildPanel = false;
    }

    public override void OnEnter()
    {
        base.OnEnter();

        m_MenuPanelCanvasGroup.blocksRaycasts = true;

        m_StartButton.Select();

        AddInputEventCallback();
    }

    public override void OnPause()
    {
        base.OnPause();

        RemoveInputEventCallback();
    }

    public override void OnResume()
    {
        base.OnResume();

        AddInputEventCallback();
    }

    public async override UniTask OnStop(bool useAnim)
    {
        await base.OnStop(useAnim);

        m_MenuPanelCanvasGroup.blocksRaycasts = false;

        RemoveInputEventCallback();
    }

    private void AddInputEventCallback()
    {
        m_StartButton.Select();

        m_StartButton.onClick.AddListener(async() =>
        {
            await PanelManager.Instance.CreateUIPanel(new ArchivePanel(), m_ChildPointRectTransform, m_MenuRootRectTransform);
        });

        m_HelpButton.onClick.AddListener(async() =>
        {
            await PanelManager.Instance.CreateUIPanel(new HelpPanel(), m_ChildPointRectTransform, m_MenuRootRectTransform);
        });

        m_AboutButton.onClick.AddListener(async() =>
        {
            await PanelManager.Instance.CreateUIPanel(new AboutPanel(), m_ChildPointRectTransform, m_MenuRootRectTransform);
        });

        m_MoreButton.onClick.AddListener(() =>
        {
            string url = "https://www.pfumobile.com/sy";

            Application.OpenURL(url);
        });

        m_ExitButton.onClick.AddListener(async() =>
        {
            await PanelManager.Instance.CreateUIPanel(new ExitPanel(), m_ChildPointRectTransform, m_MenuRootRectTransform);
        });
    }
    private void RemoveInputEventCallback()
    {
        m_StartButton.onClick.RemoveAllListeners();

        m_HelpButton.onClick.RemoveAllListeners();

        m_AboutButton.onClick.RemoveAllListeners();

        m_MoreButton.onClick.RemoveAllListeners();

        m_ExitButton.onClick.RemoveAllListeners();
    }
}
